/*
 * Copyright (C) GSX Techedu Inc. All Rights Reserved
 * Unauthorized copying of this file, via any medium is strictly prohibited
 * Proprietary and confidential
 */

package com.nime.novel.crawl.core.detail.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.nime.novel.crawl.constant.CategoryEum;
import com.nime.novel.crawl.constant.ChannelEnum;
import com.nime.novel.crawl.constant.CrawConstant;
import com.nime.novel.crawl.constant.StatusEnum;
import com.nime.novel.crawl.core.detail.DetailGetService;
import com.nime.novel.crawl.domain.BookEntity;
import com.nime.novel.crawl.domain.CrawlUrl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.stereotype.Service;

/**
 * @author liujialiang
 * @description
 * @team wuhan operational dev.
 * @date 2020/8/30 11:48 上午
 **/
@Slf4j
@Service
public class BookDetailGetServiceImpl extends DetailGetService<BookEntity> {



    @Override
    protected CrawlUrl getNewestUrl(Integer policyId) {
        CrawlUrl crawlUrl = crawlUrlService.getOldCrawlUrlByStatus(policyId, ChannelEnum.BOOK.getCode(), StatusEnum.TODO);
        if (null == crawlUrl) {
            log.info("频道更新完毕: channel={}", ChannelEnum.BOOK.getCode());
            return null;
        }
        return crawlUrl;
    }

    @Override
    protected BookEntity parseHtml(CrawlUrl crawlUrl, String html) {
        Document doc = Jsoup.parse(html);
        String bookName = doc.select("body > header > span.title").text();
        if (StringUtils.isEmpty(bookName)) {
            return null;
        }
        String author = doc.select("#book_detail > li.author").text().replace("作者：", "").trim();
        String catName = doc.select("#book_detail > li.sort").text().replace("类别：", "").trim();
        String bookStatusDesc = doc.select("#book_detail > li:nth-child(3)").text().replace("状态：", "").trim();
        String updateTime = doc.select("#book_detail > li:nth-child(4)").text().replace("更新：", "").trim();
        String description = doc.select("body > div.synopsisArea > p.review").text().trim();
        String picUrl = doc.select("#thumb > img").attr("src");
        String scoreDesc = doc.select("#score_content > div > span > em").text().trim();
        String newChapter = doc.select("body > div.synopsisArea > p:nth-child(2) > a > span").text().trim();
        CategoryEum categoryEum = CategoryEum.match(catName);
        Integer status = bookStatusDesc.contains("连载") ? 0 : 1;
        Float score;
        try {
            score = Float.valueOf(scoreDesc);
        } catch (Exception e) {
            score = 60.0f;
        }
        BookEntity bookEntity = BookEntity.builder()
            .workDirection(categoryEum == CategoryEum.NV ? 1 : 0)
            .authorName(author)
            .bookName(bookName)
            .crawlBookId(crawlUrl.getUrlHash())
            .crawlSourceId(CrawConstant.POLICY)
            .bookDesc(description)
            .catId(categoryEum.getCode())
            .catName(catName)
            .picUrl(picUrl)
            .bookStatus(status)
            .score(score)
            .status(1)
            .isVip(0)
            .lastIndexName(newChapter)
            .lastIndexUpdateTime(DateUtil.parse(updateTime))
            .build();
        return bookEntity;
    }

    @Override
    protected boolean insert(BookEntity entity) {
        // 判断存不存在
        BookEntity old = crawlBookMapper.findByCrawlBookId(entity.getCrawlBookId());
        if (old == null) {
            log.info("新书入库: {}",entity.getBookName());
            crawlBookMapper.create(entity);
            return true;
        }
        // 不想等 则说明有更新 更新书籍的url
        log.info("老书更新: {}",entity.getBookName());
        if (!ObjectUtil.equal(old.getLastIndexName(), entity.getLastIndexName())) {
            CrawlUrl curl = crawlUrlService.getByUrlHash(entity.getCrawlBookId());
            crawlUrlService.updateStatus(curl.getId(), StatusEnum.TODO.getCode());
        }
        return true;
    }
}
